package net.hancl.modules.sys.shiro.jcaptcha;

import com.octo.captcha.service.CaptchaServiceException;
import com.octo.captcha.service.captchastore.FastHashMapCaptchaStore;
import net.hancl.modules.sys.shiro.MyManageableImageCaptchaService;

import javax.servlet.http.HttpServletRequest;

public class Jcaptcha {
    public static final MyManageableImageCaptchaService captchaService = new MyManageableImageCaptchaService(
            new FastHashMapCaptchaStore(), new GMailEngine(), 180, 100000,
            75000);

    public static boolean validateResponse(HttpServletRequest request,
                                           String userCaptchaResponse) {
        if (request.getSession(false) == null)
            return false;
        boolean validated = false;
        try {
            String id = request.getSession().getId();
            validated = captchaService.validateResponseForID(id,
                    userCaptchaResponse).booleanValue();
        } catch (CaptchaServiceException e) {
            e.printStackTrace();
        }
        return validated;
    }

    public static boolean hasCaptcha(HttpServletRequest request,
                                     String userCaptchaResponse) {
        if (request.getSession(false) == null)
            return false;
        boolean validated = false;
        try {
            String id = request.getSession().getId();
            validated = captchaService.hasCapcha(id, userCaptchaResponse);
        } catch (CaptchaServiceException e) {
            e.printStackTrace();
        }
        return validated;
    }
}